package com.elaine.wxapi.wxpublic.response;

import com.alibaba.fastjson.JSON;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

public class WxApiResult {

    private Map<String, Object> attrs;

    private String json;

    /**
     * 通过json构造 WxApiResult
     * 注意返回结果不为 json 的 api（如果有的话）
     * @param jsonStr json字符串
     */
    public WxApiResult(String jsonStr) {
        this.json = jsonStr;
        try {
            Map temp = JSON.parseObject(jsonStr, Map.class);
            this.attrs = temp;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 通过 json 创建 WxApiResult 对象，等价于 new WxApiResult(jsonStr)
     * @param jsonStr json字符串
     * @return {WxApiResult}
     */
    public static WxApiResult create(String jsonStr) {
        return new WxApiResult(jsonStr);
    }

    /**
     * 获取json字符串
     * @return
     */
    public String getJsonStr() {
        return json;
    }

    /**
     * APi 请求是否成功返回
     * errorCode 为 0 时也可以表示为成功;
     * @return {boolean}
     */
    public boolean isSucceed() {
        Integer errorCode = getErrorCode();
        return (errorCode == null || errorCode == 0);
    }

    /**
     * 获取错误码
     * @return
     */
    public Integer getErrorCode() {
        return getErrorCode("errcode");
    }

    /**
     * 获取错误码
     * @return
     */
    public Integer getErrorCode(String errorCode) {
        return getInt(errorCode);
    }

    /**
     * 获取错误消息
     * @return
     */
    public String getErrorMsg() {
        return getErrorMsg("errmsg");
    }
    /**
     * 获取错误消息
     * @return
     */
    public String getErrorMsg(String errorMsg) {
        Integer errorCode = getErrorCode();
        if (errorCode != null) {
            String result = ApiReturnCode.get(errorCode);
            if (result != null) {
                return result;
            }
        }
        return (String)attrs.get(errorMsg);
    }

    public <T> T get(String name) {
        return (T)attrs.get(name);
    }

    public String getStr(String name) {
        return (String)attrs.get(name);
    }

    public Integer getInt(String name) {
        Number number = (Number) attrs.get(name);
        return number == null ? null : number.intValue();
    }

    public Long getLong(String name) {
        Number number = (Number) attrs.get(name);
        return number == null ? null : number.longValue();
    }

    public BigInteger getBigInteger(String name) {
        return (BigInteger)attrs.get(name);
    }

    public Double getDouble(String name) {
        return (Double)attrs.get(name);
    }

    public BigDecimal getBigDecimal(String name) {
        return (BigDecimal)attrs.get(name);
    }

    public Boolean getBoolean(String name) {
        return (Boolean)attrs.get(name);
    }

    public List getList(String name) {
        return (List)attrs.get(name);
    }

    public Map getMap(String name) {
        return (Map)attrs.get(name);
    }

    public Map<String, Object> getAttrs(){
        return this.attrs;
    }

    /**
     * 判断 API 请求结果失败是否由于 access_token 无效引起的
     * 无效可能的情况 error_code 值：
     * 40001 = 获取access_token时AppSecret错误，或者access_token无效(刷新后也可以引起老access_token失效)
     * 42001 = access_token超时
     * 42002 = refresh_token超时
     * 40014 = 不合法的access_token
     * @return {boolean}
     */
    public boolean isAccessTokenInvalid() {
        Integer ec = getErrorCode();
        return ec != null && (ec == 40001 || ec == 42001 || ec == 42002 || ec == 40014);
    }

}